using System.Net;
using Moq;
using NUnit.Framework;
using OpenWaves.ActivityFeed.Web.Handler;
using OpenWaves.ActivityFeed.Web.Modules;

namespace OpenWaves.ActivityFeed.UnitTests.Web
{
    [TestFixture]
    public class SubscriptionsModuleTests
    {
        [TestCase("POST", "/activityFeed/subscriptions")]
        [TestCase("DELETE", "/activityFeed/subscriptions/topic")]
        public void When_user_is_not_authenticated_Then_routes_return_Unauthorized(string verb, string route)
        {
            var userIdentityProvider = IdentityProviderMock.WithAnonymousUser();
            var browser = new Browser(new ActivityFeedApiHandler(userIdentityProvider,
                            new SubscriptionsModule(
                                new Mock<IActivityFeedService>().Object,
                                userIdentityProvider)));

            var response = browser.MakeRequest(verb, route);

            Assert.That(response.StatusCode, Is.EqualTo((int)HttpStatusCode.Unauthorized));
        }

        [Test]
        public void Given_authenticated_user_POST_to_subscriptions_subscribes_user_to_topic()
        {
            var activityFeedServiceMock = new Mock<IActivityFeedService>(MockBehavior.Strict);
            activityFeedServiceMock.Setup(service => service.Subscribe("maciek", Topics.ContainingOnly(new Topic("topic"))));

            var userIdentityProvider = IdentityProviderMock.With("maciek");
            var browser = new Browser(new ActivityFeedApiHandler(userIdentityProvider,
                            new SubscriptionsModule(
                                activityFeedServiceMock.Object,
                                userIdentityProvider)));

            browser.PostJson("/activityFeed/subscriptions", new { topic = "topic" });

            activityFeedServiceMock.VerifyAll();
        }

        [Test]
        public void Given_authenticated_user_DELETE_to_subscriptions_unsubscribes_user_from_topic()
        {
            var activityFeedServiceMock = new Mock<IActivityFeedService>(MockBehavior.Strict);
            var topic = new Topic("user/john smith");
            activityFeedServiceMock.Setup(service => service.Unsubscribe("maciek", Topics.ContainingOnly(topic)));

            var userIdentityProvider = IdentityProviderMock.With("maciek");
            var browser = new Browser(new ActivityFeedApiHandler(userIdentityProvider,
                            new SubscriptionsModule(
                                activityFeedServiceMock.Object,
                                userIdentityProvider)));

            browser.Delete("/activityFeed/subscriptions/" + Url.Encode(topic.ToString()));

            activityFeedServiceMock.VerifyAll();
        }
    }
}